package com.ruoyi.app.controller;

import com.ruoyi.app.domain.*;
import com.ruoyi.app.service.*;
import com.ruoyi.app.util.Result;
import com.ruoyi.app.util.SecureRandomGenerator;
import com.ruoyi.common.redis.service.RedisService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.ldap.Rdn;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("appointmentA")
@RestController
public class AppointmentController {
    @Autowired
    AppointmentService appointmentService;
    @Autowired
    DoctorScheduleService doctorScheduleService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    IUserService iUserService;
    @Autowired
    PatientService patientService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalAreaService hospitalAreaService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    JobTitleService jobTitleService;
    @Autowired
    RedisService redisService;
    @RequestMapping("saveAppointment")
    public Result list(@RequestBody Appointment appointment)
    {
        String s = SecureRandomGenerator.generateAlphanumericString(8);
        appointment.setAppointmentNo(s);
        appointment.setRegistrationType("专家号");
        appointment.setHtime("08:00");
        boolean b = appointmentService.saveAppointment(appointment);
        if(b == true){
            DoctorSchedule doctorSchedule = doctorScheduleService.getSelectDoctorSchedule(appointment.getScheduleId());
            doctorSchedule.setAvailableNum(doctorSchedule.getAvailableNum()-1);
            doctorScheduleService.updateSchedule(doctorSchedule);
        }
        return Result.ok("成功");
    }


    @RequestMapping("list")
    public Result list(Integer status) {
        List<Appointment> appointment = appointmentService.list(status);
        for (Appointment appointment1 : appointment) {
            User user = iUserService.getName(appointment1.getUserId());
            appointment1.setUserName(user.getNickname());

            Patient patient = patientService.getById(appointment1.getPatientId());
            appointment1.setPatientName(patient.getName());

            Hospital hospital = hospitalService.getByIdName(appointment1.getHospitalId());
            appointment1.setHospitalName(hospital.getName());
            appointment1.setHospitalAddressA(hospital.getAddress());

            HospitalArea hospitalArea = hospitalAreaService.getByIdName(appointment1.getHospitalAddress());
            appointment1.setHospitalAreaName(hospitalArea.getName());
            appointment1.setHospitalAreaAddress(hospitalArea.getAddress());

            Department department = departmentService.getById(appointment1.getDepartmentId());
            appointment1.setDepartmentName(department.getName());

            Doctor doctor = doctorService.selectDoctorById(appointment1.getDoctorId());
            appointment1.setDoctorName(doctor.getName());

            JobTitle jobTitle = jobTitleService.getById(appointment1.getJobTitle());
            appointment1.setJobTitleName(jobTitle.getName());

            DoctorSchedule doctorSchedule = doctorScheduleService.getSelectDoctorSchedule(appointment1.getScheduleId());
            appointment1.setScheduleName(doctorSchedule.getScheduleDate());
        }
        return Result.ok(appointment);
    }

    @RequestMapping("getUpdateById")
    public void getUpdateById(Long id){
        Appointment appointment = appointmentService.getUpdateById(id);
        appointment.setStatus(2);
        appointmentService.updateAppointment(appointment);
    }
}
